package com.aihna.mall.controller.api;

import cn.hutool.core.util.ObjectUtil;
import com.aihna.common.core.domain.R;
import com.aihna.common.core.domain.model.LoginUser;
import com.aihna.common.satoken.utils.LoginHelper;
import com.aihna.common.tenant.helper.TenantHelper;
import com.aihna.common.web.core.BaseController;
import com.aihna.mall.domain.vo.StoreUserInfoVo;
import com.aihna.mall.domain.vo.StoreUserVo;
import com.aihna.mall.service.IStoreUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 客户端-会员用户
 *
 * @author Joseph.Chen
 * @date 2024-04-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/mall/storeUser")
public class ApiStoreUserController extends BaseController {
    private final IStoreUserService storeUserService;

    @GetMapping("/getInfo")
    public R<StoreUserInfoVo> getInfo() {
        StoreUserInfoVo userInfoVo = new StoreUserInfoVo();
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (TenantHelper.isEnable() && LoginHelper.isSuperAdmin()) {
            // 超级管理员 如果重新加载用户信息需清除动态租户
            TenantHelper.clearDynamic();
        }
        StoreUserVo user = storeUserService.queryById(loginUser.getUserId());
        if (ObjectUtil.isNull(user)) {
            return R.fail("没有权限访问用户数据!");
        }
        userInfoVo.setUser(user);
        userInfoVo.setPermissions(loginUser.getMenuPermission());
        userInfoVo.setRoles(loginUser.getRolePermission());
        return R.ok(userInfoVo);
    }

    @GetMapping("/profile")
    public R<StoreUserVo> profile() {
        StoreUserVo user = storeUserService.queryById(LoginHelper.getUserId());
        return R.ok(user);
    }


}
